<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能童话书</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
            color: white;
        }

        .header h1 {
            font-size: 3rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            font-size: 1.2rem;
            opacity: 0.9;
        }

        .main-content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-bottom: 40px;
        }

        .story-generator {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }

        .story-display {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            max-height: 600px;
            overflow-y: auto;
        }

        .section-title {
            font-size: 1.5rem;
            margin-bottom: 20px;
            color: #333;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }

        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 12px;
            border: 2px solid #ddd;
            border-radius: 10px;
            font-size: 1rem;
            transition: border-color 0.3s;
        }

        .form-group input:focus,
        .form-group select:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #667eea;
        }

        .theme-selector {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-top: 10px;
        }

        .theme-option {
            padding: 15px;
            border: 2px solid #ddd;
            border-radius: 10px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
            background: #f8f9fa;
        }

        .theme-option:hover {
            border-color: #667eea;
            background: #e3f2fd;
        }

        .theme-option.selected {
            border-color: #667eea;
            background: #667eea;
            color: white;
        }

        .theme-icon {
            font-size: 2rem;
            margin-bottom: 5px;
        }

        .generate-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 1.1rem;
            font-weight: bold;
            cursor: pointer;
            transition: transform 0.3s;
        }

        .generate-btn:hover {
            transform: translateY(-2px);
        }

        .generate-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }

        .loading-spinner {
            width: 40px;
            height: 40px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .story-content {
            line-height: 1.8;
            font-size: 1.1rem;
            color: #444;
        }

        .story-meta {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 10px;
            margin-bottom: 20px;
            border-left: 4px solid #667eea;
        }

        .story-meta h3 {
            color: #667eea;
            margin-bottom: 10px;
        }

        .meta-info {
            display: flex;
            justify-content: space-between;
            font-size: 0.9rem;
            color: #666;
        }

        .error-message {
            background: #ffebee;
            color: #c62828;
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            border-left: 4px solid #c62828;
        }

        .success-message {
            background: #e8f5e8;
            color: #2e7d32;
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            border-left: 4px solid #2e7d32;
        }

        .features {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            margin-top: 30px;
        }

        .features-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }

        .feature-card {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 15px;
            text-align: center;
            border: 2px solid transparent;
            transition: all 0.3s;
        }

        .feature-card:hover {
            border-color: #667eea;
            transform: translateY(-5px);
        }

        .feature-icon {
            font-size: 3rem;
            margin-bottom: 15px;
        }

        @media (max-width: 768px) {
            .main-content {
                grid-template-columns: 1fr;
            }
            
            .header h1 {
                font-size: 2rem;
            }
            
            .theme-selector {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🎭 智能童话书</h1>
            <p>基于ERNIE模型的智能故事生成系统</p>
        </div>

        <div class="main-content">
            <div class="story-generator">
                <h2 class="section-title">📝 创作您的故事</h2>
                
                <form id="storyForm">
                    <div class="form-group">
                        <label for="characterName">主角名字</label>
                        <input type="text" id="characterName" name="character_name" 
                               placeholder="例如：小兔子、小熊..." maxlength="20">
                    </div>

                    <div class="form-group">
                        <label>故事主题</label>
                        <div class="theme-selector">
                            {% for theme_id, theme_info in themes.items() %}
                            <div class="theme-option" data-theme="{{ theme_id }}">
                                <div class="theme-icon">{{ theme_info.icon }}</div>
                                <div>{{ theme_info.name }}</div>
                                <small>{{ theme_info.description }}</small>
                            </div>
                            {% endfor %}
                        </div>
                        <input type="hidden" id="selectedTheme" name="theme" value="adventure">
                    </div>

                    <div class="form-group">
                        <label for="userPrompt">故事想法 (可选)</label>
                        <textarea id="userPrompt" name="user_prompt" rows="3" 
                                  placeholder="描述您希望的故事情节或元素..."></textarea>
                    </div>

                    <button type="submit" class="generate-btn" id="generateBtn">
                        🎨 生成童话故事
                    </button>
                </form>

                <div class="loading" id="loading">
                    <div class="loading-spinner"></div>
                    <p>正在创作您的专属童话故事...</p>
                </div>
            </div>

            <div class="story-display">
                <h2 class="section-title">📖 您的童话故事</h2>
                
                <div id="storyResult">
                    <div style="text-align: center; color: #666; margin-top: 50px;">
                        <div style="font-size: 4rem; margin-bottom: 20px;">📚</div>
                        <p>请在左侧创作您的第一个童话故事</p>
                        <p style="margin-top: 10px; font-size: 0.9rem;">
                            每个故事都精心创作，带给您完整的阅读体验
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <div class="features">
            <h2 class="section-title">✨ 应用特色</h2>
            <div class="features-grid">
                <div class="feature-card">
                    <div class="feature-icon">🎭</div>
                    <h3>精彩故事结构</h3>
                    <p>每个故事都精心设计，情节跌宕起伏，引人入胜</p>
                </div>
                <div class="feature-card">
                    <div class="feature-icon">🤖</div>
                    <h3>智能故事生成</h3>
                    <p>基于ERNIE模型，生成逻辑清晰、情感丰富的完整故事</p>
                </div>
                <div class="feature-card">
                    <div class="feature-icon">🎨</div>
                    <h3>多样主题选择</h3>
                    <p>冒险、友谊、成长、魔法四大主题，满足不同阅读需求</p>
                </div>
                <div class="feature-card">
                    <div class="feature-icon">👶</div>
                    <h3>寓教于乐</h3>
                    <p>每个故事都蕴含深刻道理，在娱乐中传递正能量</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 主题选择功能
        document.querySelectorAll('.theme-option').forEach(option => {
            option.addEventListener('click', function() {
                // 移除其他选中状态
                document.querySelectorAll('.theme-option').forEach(opt => {
                    opt.classList.remove('selected');
                });
                
                // 添加选中状态
                this.classList.add('selected');
                
                // 更新隐藏字段
                document.getElementById('selectedTheme').value = this.dataset.theme;
            });
        });

        // 默认选中第一个主题
        document.querySelector('.theme-option').classList.add('selected');

        // 表单提交处理
        document.getElementById('storyForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const generateBtn = document.getElementById('generateBtn');
            const loading = document.getElementById('loading');
            const storyResult = document.getElementById('storyResult');
            
            // 显示加载状态
            generateBtn.disabled = true;
            loading.style.display = 'block';
            
            try {
                // 收集表单数据
                const formData = {
                    character_name: document.getElementById('characterName').value.trim(),
                    theme: document.getElementById('selectedTheme').value,
                    user_prompt: document.getElementById('userPrompt').value.trim()
                };
                
                // 发送请求
                const response = await fetch('/generate_story', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(formData)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    // 显示成功结果
                    displayStory(result.story);
                    showMessage(result.message, 'success');
                } else {
                    // 显示错误信息
                    showMessage(result.error || '故事生成失败', 'error');
                }
                
            } catch (error) {
                console.error('请求失败:', error);
                showMessage('网络错误，请检查连接后重试', 'error');
            } finally {
                // 恢复按钮状态
                generateBtn.disabled = false;
                loading.style.display = 'none';
            }
        });

        // 显示故事
        function displayStory(story) {
            const storyResult = document.getElementById('storyResult');
            
            storyResult.innerHTML = `
                <div class="story-meta">
                    <h3>${story.title}</h3>
                    <div class="meta-info">
                        <span>📅 ${story.created_at}</span>
                        <span>📝 ${story.word_count} 字</span>
                        <span>⏱️ 约 ${story.reading_time} 分钟</span>
                    </div>
                </div>
                <div class="story-content">
                    ${story.content.split('\n\n').map(paragraph => 
                        `<p style="margin-bottom: 15px;">${paragraph}</p>`
                    ).join('')}
                </div>
            `;
        }

        // 显示消息
        function showMessage(message, type) {
            const messageDiv = document.createElement('div');
            messageDiv.className = type === 'success' ? 'success-message' : 'error-message';
            messageDiv.textContent = message;
            
            const storyResult = document.getElementById('storyResult');
            storyResult.insertBefore(messageDiv, storyResult.firstChild);
            
            // 3秒后自动移除消息
            setTimeout(() => {
                if (messageDiv.parentNode) {
                    messageDiv.parentNode.removeChild(messageDiv);
                }
            }, 3000);
        }

        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            console.log('🎭 智能童话书应用已加载');
            console.log('✨ 支持智能故事生成');
        });
    </script>
</body>
</html>